package com.fhz.kotlintest.databinding.http

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 时间:2022/1/4
 * @author msfeng
 * 简述: TODO
 */
object HttpClient  {

    private const val READ_OUT_TIME = 10_000L
    private const val WRITE_OUT_TIME = 10_1000L
    private const val CONNECT_OUT_TIME = 10_1000L

    private val mRetrofit by lazy {
        Retrofit.Builder().baseUrl("http://www.qubaobei.com/")
                .client(mClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
    }

    /**
     * 配置client
     */
    private val mClient by lazy {
        OkHttpClient.Builder().readTimeout(READ_OUT_TIME, TimeUnit.SECONDS)
                .writeTimeout(WRITE_OUT_TIME, TimeUnit.SECONDS)
                .connectTimeout(CONNECT_OUT_TIME, TimeUnit.SECONDS)
                .addInterceptor(mLogInterceptor)
                .build()
    }

    /**
     * 日志打印拦截器
     */
    private val mLogInterceptor by lazy {
        HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY
        }
    }

    /**
     * 创建接口Api
     */
    fun <T> createApi(service: Class<T>): T {
        return mRetrofit.create(service)
    }

}